package com.example.mcdonaldapplication.activity

import android.annotation.SuppressLint
import android.content.Context
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.os.Looper
import android.util.Log
import android.widget.Toast
import com.example.mcdonaldapplication.databinding.ActivityBuyGoodsBinding
import com.example.mcdonaldapplication.info.GoodsInfo
import com.example.mcdonaldapplication.info.ServerIP
import okhttp3.FormBody
import okhttp3.OkHttpClient
import okhttp3.Request
import org.json.JSONObject
import kotlin.concurrent.thread

class BuyGoodsActivity : AppCompatActivity() {
    private val hotGoodsList = ArrayList<GoodsInfo>()
    private lateinit var binding: ActivityBuyGoodsBinding
    private var uid = -1
    private val serverIp = ServerIP.getIp()

    @SuppressLint("SetTextI18n")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityBuyGoodsBinding.inflate(layoutInflater)
        setContentView(binding.root)
        supportActionBar?.hide()
        initHotGoodsList()
        val gid = intent.getIntExtra("gid",0)
        if(gid == 5 || gid == 6 || gid == 6 || gid == 7 || gid == 8){
            for(i in 0 until hotGoodsList.size){
                if(hotGoodsList[i].gid == gid){
                    binding.goodPrice.text = hotGoodsList[i].price
                    binding.sales.text = "100+"
                    binding.name.text = hotGoodsList[i].name
                    binding.tvContent.text = hotGoodsList[i].content
                }
            }
        }else{
            binding.goodPrice.text = intent.getStringExtra("price")
            binding.sales.text = "100+"
            binding.name.text = intent.getStringExtra("name")
            binding.tvContent.text = intent.getStringExtra("content")
        }

        binding.btnBuy.setOnClickListener {
            buyGoods(gid)
        }
    }

    private fun initHotGoodsList() {
        hotGoodsList.add(GoodsInfo(8,"10","早餐卡","早餐卡",0,100,10,"---",0))
        hotGoodsList.add(GoodsInfo(5,"50","浓情蜜意双人餐","浓情蜜意双人餐",0,100,50,"---",0))
        hotGoodsList.add(GoodsInfo(6,"30","三人成行三人餐","三人成行三人餐",0,100,30,"---",0))
        hotGoodsList.add(GoodsInfo(7,"120","欢聚时刻多人餐","欢聚时刻多人餐",0,100,120,"---",0))
    }

//    override fun process(str: String) {
//        Log.d("oh_god",str)
//    }

    private fun buyGoods(gid: Int){
        val prefs = getSharedPreferences("user", Context.MODE_PRIVATE)
        uid = prefs.getInt("uid",-1)
        Log.d("buy_uid",uid.toString())
        thread {
            //post方式提交的数据

            val goodJson = "[{\"gid\":$gid,\"num\":\"1\",\"type\":\"1\"}]"
            val formBody: FormBody = FormBody.Builder()
                .add("uid", uid.toString())
                .add("tid", "")
                .add("good",goodJson)
                .build()
            val client = OkHttpClient()
            val request = Request.Builder()
                .url("http://${serverIp}/goods/buyGoods")
                .post(formBody)
                .build()
            val response = client.newCall(request).execute()
            val responseData = response.body.string()
            Log.d("buy_json",goodJson)
            parseJsonBuyGoods(responseData)
        }
    }

    private fun parseJsonBuyGoods(json: String){
        val jsonObject = JSONObject(json)
        Looper.prepare()
        Toast.makeText(this,jsonObject.optString("message"), Toast.LENGTH_SHORT).show()
        finish()
        Looper.loop()
    }

}